package com.liusand.admin.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.liusand.admin.mapper.BlogMapper;
import com.liusand.admin.mapper.CategoryMapper;
import com.liusand.admin.service.CategoryService;
import com.liusand.xo.pojo.Blog;
import com.liusand.xo.pojo.Category;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author LiuSha
 */
@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private BlogMapper blogMapper;

    @Override
    public boolean add(Category category) {

        Category categoryInDb = findByName(category.getCategoryName());
        if (categoryInDb != null) {
//            if (comeBack(categoryInDb)) {
//                return true;
//            }
            log.info("分类名称\"{}\"重复", category.getCategoryName());
            return false;
        }

//        String categoryId = IdUtil.getSnowflakeNextIdStr();
//        category.setCategoryId(categoryId);
        return categoryMapper.insert(category) > 0;
    }

    private boolean comeBack(Category categoryInDb) {
        if (categoryInDb.getDeleted() == null) {
            return false;
        }
        categoryInDb.setCreated(LocalDateTime.now());
        categoryInDb.setUpdated(LocalDateTime.now());
        categoryInDb.setDeleted(null);
        categoryMapper.updateById(categoryInDb);
        return true;

    }

    @Override
    public boolean deleteById(String id) {
        Category category = findById(id);
        if (category == null) {
            return false;
        }
        //todo 需要判断分类ID是否被使用

        if (isUsed(category)) {
            return false;
        }

        category.setDeleted(LocalDateTime.now());

        int i = categoryMapper.updateById(category);
        return i > 0;
    }

    private boolean isUsed(Category category) {
        LambdaQueryWrapper<Blog> wrapper = new LambdaQueryWrapper<>();
        wrapper.isNull(Blog::getDeleted);
        List<Blog> lists = blogMapper.selectList(wrapper);
        for (Blog b : lists) {
            if (b.getCategoryId().equals(category.getCategoryId())) {
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean update(Category category) {
        Category categoryInDb = findByName(category.getCategoryName());
        if (categoryInDb != null) {
            log.info("分类名称\"{}\"重复", category.getCategoryName());
            return false;
        }
        category.setUpdated(LocalDateTime.now());
        int i = categoryMapper.updateById(category);
        return i > 0;
    }

    @Override
    public List<Category> findAll() {
        LambdaQueryWrapper<Category> w = new LambdaQueryWrapper<>();
        w.isNull(Category::getDeleted);
        w.orderByDesc(Category::getCreated);

        return categoryMapper.selectList(w);
    }

    @Override
    public Category findById(String id) {
        LambdaQueryWrapper<Category> w = new LambdaQueryWrapper<>();
        w.eq(Category::getCategoryId, id);
        w.isNull(Category::getDeleted);

        return categoryMapper.selectOne(w);
    }

    @Override
    public Category findByName(String name) {
        LambdaQueryWrapper<Category> w = new LambdaQueryWrapper<>();
        w.eq(Category::getCategoryName, name);

        return categoryMapper.selectOne(w);
    }
}
